namespace Nico { 
    
    [GtkTemplate (ui="/de/grindhold/nico/ui/nico_client_app_connect.ui")]
    public class ClientAppConnect : Gtk.Box { 

        public signal void connected_signal(Nico.Client client);
        public signal void error_signal(string message);

        [GtkChild]
        private Gtk.Entry host;

        [GtkChild]
        private Gtk.Entry port;

        private bool connected = false;

        private weak Nico.ClientView client_view;


        public void ClientAppConnect(Nico.ClientView client_view) {
            this.client_view = client_view;
        }

        [GtkCallback]
        private void connect_clicked() {
            if(!this.connected) {
                try {
                    int tmp = int.parse(this.port.text);
                    Nico.Client client = new Nico.Client(this.host.text, (uint16) tmp, GLib.Environment.get_user_name());
                    this.connected = true;
                    this.connected_signal(client);
                }
                catch(Error e) {
                    this.error_signal("Connection failed");
                }       
            }
        }
    }
}
